package cn.kgc.springboot.controller;

import cn.kgc.springboot.entity.User;
import cn.kgc.springboot.util.JsonUtil;
import cn.kgc.springboot.util.RedisUtil;
import cn.kgc.springboot.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * @author mosin
 * date 2021/12/16
 * @version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private Result result;

    @RequestMapping("/tologin")
    public String toLogin(){
        return  "login";
    }

    // @RequestMapping("/login")
    // public String login(User user, HttpSession session){
    //     session.setAttribute("user", user);
    //     return "index" ;
    // }

    @RequestMapping("/login")
    @ResponseBody
    public Object login(User user){  //token  uuid
        //先验证用户名和密码是否正确

        //生成用于的token 用于身份验证
        String token = UUID.randomUUID().toString().replace("-", "");
        redisUtil.setString(token, JsonUtil.toString(user));

        System.out.println("uuid:"+token);

        result.setCode(200);
        result.setData(token);
        result.setMsg("login ok");
        return result ;
    }

    @RequestMapping("/toregister")
    public String toRegister(){
        return  "register";
    }

    @RequestMapping("/toindex")
    public String toIndex(){
        return  "index";
    }

    @RequestMapping("/del")
    @ResponseBody
    public String del(){
        return  "delete ok";
    }

}
